<template>
  <div>
    <!-- 搜索 -->
    <van-search
      show-action
      v-model="searchtext"
      shape="round"
      background="#fff"
      :autofocus="true"
      placeholder="卡姿兰大眼睛"
      @keydown.stop.enter="search"
    >
      <template #left>
        <div class="address" @click="this.$router.go(-1)">
          <van-icon name="arrow-left" />
        </div>
      </template>
      <template #action>
        <div class="my" @click="search">搜索</div>
      </template>
    </van-search>

    <!-- 历史搜索 -->
    <van-cell-group>
      <van-cell title="历史搜搜" value="隐藏" />
      <ul class="HistorySearch">
        <li v-for="item in HistorySearch" :key="item" @click="searchCklick">
          {{ item }}
        </li>
      </ul>
    </van-cell-group>

    <!-- 热门搜索 -->
    <van-cell-group>
      <van-cell title="热门搜索" value="隐藏" />
      <ul class="HistorySearch">
        <li v-for="item in HistorySearch" :key="item" @click="searchCklick">
          {{ item }}
        </li>
      </ul>
    </van-cell-group>
  </div>
</template>

<script>
import { proSearch } from "../server/search.js";
export default {
  data() {
    return {
      HistorySearch: [],
      searchtext: "",
    };
  },
  created() {
    this.HistorySearch = this.$store.state.HistorySearch;
  },
  methods: {
    search() {
      if (!this.searchtext) return;
      proSearch({ count: 1, limitNum: 10, keyword: this.searchtext }).then(
        (res) => {
          this.$store.commit("setSearchList", res.data);
          this.$store.commit("setHistorySearch", this.searchtext);
          this.$router.push(`/searchList?search=${this.searchtext}`);
        }
      );
    },
    searchCklick(e) {
      this.searchtext = e.target.innerText;
      this.search();
    },
  },
};
</script>

<style scoped>
.HistorySearch {
  display: flex;
  flex-wrap: wrap;
}
.HistorySearch > li {
  font-size: 0.875rem;
  text-align: center;
  border-radius: 0.3125rem;
  background-color: #e0e0e0;
  color: red;
  margin: 0.3125rem;
  padding: 0.3125rem;
}
.van-cell-group {
  margin-top: 1.25rem;
}
.van-search {
  height: 3.125rem;
  width: 100%;
  padding: 0 0.625rem;
}
.address {
  margin-right: 0.625rem;
}
.my,
.address {
  color: #666;
}
.van-search__content {
  border-radius: 1.875rem;
}

:deep(.van-grid-item__content) {
  padding: 0;
}
</style>
